<template>
  <div>
    <el-button @click="openDialog">添加</el-button>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      row-key="id"
      border
    >
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column prop="catename" label="分类名称" align="center">
      </el-table-column>
      <el-table-column label="分类图片" align="center">
        <template slot-scope="scope">
          <img
            width="100"
            height="100"
            v-if="scope.row.img"
            :src="$host + scope.row.img"
          />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            circle
            size="mini"
            @click="edit(scope.row.id)"
            icon="el-icon-edit"
          ></el-button>
          <el-button
            type="danger"
            circle
            size="mini"
            @click="del(scope.row.id)"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="popType + '分类'"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <cate-info :transData='transData'></cate-info>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getCateList, getCateInfoById } from "../../request";
import cateInfo from "./index1";
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      // 弹窗类型：新增或者修改
      popType: "新增",
      transData: {},
    };
  },
  components: {
    cateInfo,
  },
  created() {
    // 获取分类列表
    this.getData();
  },
  methods: {
    // 获取表格的数据
    async getData() {
      let res = await getCateList();
      console.log(res);
      this.tableData = res.data.list;
    },
    // 编辑
    async edit(id) {
      this.popType = "编辑";
      let res = await getCateInfoById(id);
      this.transData = res.data.list;
      this.dialogVisible = true;
    },
    // 删除,需要先弹窗二次确认
    del(id) {
      this.$confirm("确认关闭？")
        .then((_) => {
          // 确认删除
          this.doDel(id);
        })
        .catch((_) => {
          // 关闭弹窗
        });
    },
    doDel(id) {
      this.http.post("/catedelete", { id }).then((res) => {
        this.tableData = res.data.list;
      });
    },
    // 切换页面
    handleCurrentChange(val) {
      this.getData();
    },
    openDialog() {
      this.popType = "新增";
      this.transData = {
        pid: 0,
        img: "",
        catename: "",
        status: 1,
      };
      this.dialogVisible = true;
    },
  },
};
</script>
<style lang="">
</style>